define(['jquery', 'bootstrap', 'backend', 'table', 'form' , 'layer'], function ($, undefined, Backend, Table, Form ,Layer) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'campus_class_course_arranging/index?course_id='+Config.course_id,
                    add_url: 'campus_class_course_arranging/add?course_id='+Config.course_id,
                    edit_url: 'campus_class_course_arranging/edit',
                    del_url: 'campus_class_course_arranging/del',
                    multi_url: 'campus_class_course_arranging/multi',
                    import_url: 'campus_class_course_arranging/import',
                    table: 'campus_class_course_arranging',
                }
            });

            //绑定事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var panel = $($(this).attr("href"));
                if (panel.size() > 0) {
                    Controller.table[panel.attr("id")].call(this);
                    $(this).on('click', function (e) {
                        $($(this).attr("href")).find(".btn-refresh").trigger("click");
                    });
                }
                //移除绑定的事件
                $(this).unbind('shown.bs.tab');
            });

            //必须默认触发shown.bs.tab事件
            $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");
        },
        // 初始化表格
        table: {
            first: function () {
                // 表格1
                var table1 = $("#table1");
                table1.bootstrapTable({
                    url: 'campus_class_course_arranging/table1',
                    extend: {
                        index_url: '',
                        add_url: 'campus_class_course_arranging/add?course_id='+Config.course_id,
                        edit_url: 'campus_class_course_arranging/edit',
                        del_url: '',
                        multi_url: '',
                        table: '',
                    },
                    toolbar: '#toolbar1',
                    sortName: 'id',
                    search: false,
                    columns: [
                        [
                            {checkbox: true},
                            {field: 'id', title: __('Id')},
                            {field: 'teacher_id', title: __('Teacher_id'), operate: 'LIKE'},
                            {field: 'classroom_id', title: __('Classroom_id')},
                            {field: 'content', title: __('Content'), operate: 'LIKE'},
                            {field: 'campusclassroom.name', title: __('Campusclassroom.name'), operate: 'LIKE'},
                            {field: 'campuscoursemanagement.name', title: __('Campuscoursemanagement.name'), operate: 'LIKE'},
                            {field: 'operate', title: __('Operate'), table: table1, events: Table.api.events.operate, formatter: Table.api.formatter.operate},
                        ]
                    ]
                });

                // 为表格1绑定事件
                Table.api.bindevent(table1);
            },
            second: function () {

                // 表格2
                var table2 = $("#table2");
                table2.bootstrapTable({
                    url: 'campus_class_course_arranging/table2',
                    extend: {
                        index_url: '',
                        add_url: 'campus_goods/purchase_create?type=1',
                        //add_url: 'goods/create_log?type=1',
                        edit_url: '',
                        del_url: '',
                        multi_url: '',
                        table: '',
                    },
                    toolbar: '#toolbar2',
                    sortName: 'id',
                    search: false,
                    columns: [
                        [
                            {field: 'id', title: 'ID'},
                            {field: 'inout_time', title: __('Inout_time'), formatter: Table.api.formatter.datetime, datetimeFormat:"YYYY-MM-DD", operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                            {field: 'goods_name', title: __('Title')},
                            {field: 'type',title: __('Type'),searchList: {'0': __("Type 0"), '1': __("Type 1"), '2': __("Type 2"), '3': __("Type 3"), '4': __("Type 4")},formatter: Table.api.formatter.normal},
                            {
                                field: 'num',
                                title: __('Inout_num'),
                                operate: false,
                                formatter: function(row,data){
                                    console.log(111111)
                                    if(data.type == 1 || data.type == 3){
                                        data.num = '+'+data.num;
                                        console.log('+'+data.num)
                                    }else if(data.type == 4){
                                        if(data.num > 0){
                                            data.num = '+'+data.num;
                                        }
                                    }else{
                                        data.num = '-'+data.num;
                                    }
                                    let html = data.num
                                    return html
                                }
                            },
                            {field: 'role_type',title: __('Role_type'),searchList: {'0': __("Role_type 0"),'1': __("Role_type 1"), '2': __("Role_type 2"), '3': __("Role_type 3")},formatter: Table.api.formatter.normal},
                            {field: 'role_name', title: __('Role_name')},
                            //{field: 'sn', title: __('Sn'),formatter: Controller.api.formatter.sn},
                            {field: 'sn', title: __('Sn'),
                                formatter: function(row,data){
                                    console.log(row,data)
                                    let html = '<div class="btn_sn" data-sn="'+data.sn+'" data-id="'+data.id+'">'+data.sn+'</div>'
                                    //`<span class="icon-open">+</span>${data.goodsspec.length}项`
                                    return html
                                }},
                            {field: 'agent_name', title: __('Agent_name')},
                            {
                                field: 'buttons',
                                // width: "120px",
                                title: '',
                                table: table2,
                                events: Table.api.events.operate,
                                buttons: [
                                    {
                                        name: 'ajax',
                                        text: __('查看详情'),
                                        title: __('查看详情'),
                                        classname: 'btn btn-xs btn-success btn-magic btn-dialog',
                                        // icon: 'fa fa-magic',
                                        url: 'campus_goods/log_detail/ids/{ids}',
                                        extend: 'data-area=["100%","100%"]',
                                    }
                                ],
                                formatter: Table.api.formatter.buttons
                            },
                            //操作栏,默认有编辑、删除或排序按钮,可自定义配置buttons来扩展按钮
                            //{
                            //    field: 'operate',
                            //    width: "150px",
                            //    title: __('Operate'),
                            //    table: table2,
                            //    events: Table.api.events.operate,
                            //    buttons: [
                            //        {
                            //            name: '查看详情',
                            //            title: __('查看详情'),
                            //            classname: 'btn btn-xs btn-info btn-click',
                            //            //icon: 'fa fa-leaf',
                            //            // dropdown: '更多',//如果包含dropdown，将会以下拉列表的形式展示
                            //            click: function (data) {
                            //                Layer.alert("点击按钮执行的事件");
                            //            }
                            //        },
                            //    ],
                            //    formatter: Table.api.formatter.operate
                            //},
                        ]
                    ]
                });
                $(document).on('click','.btn_sn' ,function (e) {
                    console.log( $(this).data('sn'))
                    Layer.open({
                        type: 2,
                        content:'campus_goods/purchaseDetail?sn='+$(this).data('sn')+'&ids='+$(this).data('id'),
                        area:['100%','100%'],
                        //content: Config.url
                    })
                })
                // 为表格2绑定事件
                Table.api.bindevent(table2);
            }
        },
        add: function () {
            $("#c-course_id").data("params", function (obj) {
                return {
                    custom: {
                        id: ['in',$("#course_id_str").val()],
                    }
                };
            });
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                $(document).on("change", "input[name='row[arranging_way]']", function(){
                    var arranging_way = $(this).val();
                    if(arranging_way == '1'){
                        $(".arranging_way_1").show();
                        $(".arranging_way_2").hide();
                        $(".end_way_1").show();
                    }else if(arranging_way == '2'){
                        $(".arranging_way_1").hide();
                        $(".arranging_way_2").show();
                        $(".end_way_1").hide();
                        $(".end_way_2").hide();
                    }
                });
                $(document).on("change", "input[name='row[repeated_way]']", function(){
                    var repeated_way = $(this).val();
                    if(repeated_way == '1' || repeated_way == '2'){
                        $("#class_time_1").show();
                        $("#class_time_2").hide();
                    }else if(repeated_way == '3' || repeated_way == '4'){
                        $("#class_time_1").hide();
                        $("#class_time_2").show();
                    }
                });
                $(document).on("change", "input[name='row[end_way]']", function(){
                    var end_way = $(this).val();
                    if(end_way == '1'){
                        $(".end_way_1").show();
                        $(".end_way_2").hide();
                    }else if(end_way == '2'){
                        $(".end_way_1").hide();
                        $(".end_way_2").show();
                    }
                });
                $(document).on("fa.event.appendfieldlist", "#add-form .btn-append", function (e, obj) {
                    //Form.events.selectpage(obj);
                    Form.events.datetimepicker(obj);
                });
                $(document).on("fa.event.appendfieldlist", "#edit-form .btn-append", function (e, obj) {
                    Form.events.selectpage(obj);
                    Form.events.datetimepicker(obj);
                });
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});